package com.ruikang.slidemenutest;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import com.ruikang.slidemenutest.entity.MyData;
import com.ruikang.slidemenutest.slidemenu.MenuDrawer;
import com.ruikang.slidemenutest.slidemenu.Position;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements View.OnClickListener {

    private MenuDrawer menuDrawer;
    private int mActiveViewId;
    private static final String STATE_MENUDRAWER = "net.simonvt.menudrawer.samples.WindowSample.menuDrawer";
    private static final String STATE_ACTIVE_VIEW_ID = "net.simonvt.menudrawer.samples.WindowSample.activeViewId";

    private PinnedSectionListView listView;
    private MyAdapter adapter;
    private List<MyData> datas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);

        if (savedInstanceState != null) {
            mActiveViewId = savedInstanceState.getInt(STATE_ACTIVE_VIEW_ID);
        }

        menuDrawer = MenuDrawer.attach(this, MenuDrawer.Type.BEHIND, Position.LEFT,MenuDrawer.MENU_DRAG_WINDOW);
        //设置主页View
        menuDrawer.setContentView(R.layout.activity_main);
        //设置菜单View
        menuDrawer.setMenuView(R.layout.menu_scrollview);
        //设置指示View
//        menuDrawer.setActiveView();

        menuDrawer.setMenuSize((ScreenUtils.getScreenWidth(this)*3)/4);

        TextView activeView = (TextView) findViewById(mActiveViewId);
        if (activeView != null) {
            menuDrawer.setActiveView(activeView);
//            mContentTextView.setText("Active item: " + activeView.getText());
        }
        menuDrawer.peekDrawer();

        findViewById(R.id.item1).setOnClickListener(this);
        findViewById(R.id.item2).setOnClickListener(this);
        findViewById(R.id.item3).setOnClickListener(this);
        findViewById(R.id.item4).setOnClickListener(this);
        findViewById(R.id.item5).setOnClickListener(this);
        findViewById(R.id.item6).setOnClickListener(this);

        listView = (PinnedSectionListView) findViewById(R.id.list_view);

        datas = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            MyData myData = new MyData(MyData.SECTION,"A-->"+i);
            datas.add(myData);
            for (int j = 0; j < 15; j++) {
                MyData myData2 = new MyData(MyData.ITEM,"item------>"+j);
                datas.add(myData2);
            }
        }

        adapter = new MyAdapter(datas,this);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                long itemId = adapter.getItemId(position);
                MyData myData = datas.get((int) itemId);
                Log.d("debug",myData.toString());
                if(myData.type == MyData.SECTION){
                    if(!menuDrawer.isMenuVisible()){
                        menuDrawer.openMenu();
                    }
                }else if(myData.type == MyData.ITEM){
                    Toast.makeText(MainActivity.this,"点击了-->"+myData.text,Toast.LENGTH_SHORT);
                }
            }
        });
    }

    @Override
    protected void onRestoreInstanceState(Bundle inState) {
        super.onRestoreInstanceState(inState);
        menuDrawer.restoreState(inState.getParcelable(STATE_MENUDRAWER));
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable(STATE_MENUDRAWER, menuDrawer.saveState());
        outState.putInt(STATE_ACTIVE_VIEW_ID, mActiveViewId);
    }

    @Override
    public void onBackPressed() {
        final int drawerState = menuDrawer.getDrawerState();
        if (drawerState == MenuDrawer.STATE_OPEN || drawerState == MenuDrawer.STATE_OPENING) {
            menuDrawer.closeMenu();
            return;
        }

        super.onBackPressed();
    }

    @Override
    public void onClick(View v) {
        menuDrawer.setActiveView(v);
        menuDrawer.closeMenu();
        mActiveViewId = v.getId();
    }
}
